package com.adcc.swingui.conf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.adcc.swingui.bean.PanelBean;
import com.adcc.swingui.bean.PanelComponentBean;

/**
 * 面板配置
 * 
 * @author charlie
 * 
 */
public class PanelConf {
	private static Logger logger = Logger.getLogger(PanelConf.class);
	private static Map<String, PanelBean> panelConf = new HashMap<String, PanelBean>();

	public static void initConf(String path) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					PanelBean bean = new PanelBean();
					Element panelBar = (Element) i.next();
					List<PanelComponentBean> componentList = new ArrayList<PanelComponentBean>();
					if (panelBar.getName().equals("panel")) {
						bean.setName(panelBar.attributeValue("name"));
						bean.setLayoutConf(panelBar
								.attributeValue("layoutConf"));
						bean.setMenuBar(panelBar.attributeValue("menuBar")
								.split(","));
						bean.setMenuBarConf(panelBar
								.attributeValue("menuBarConf"));
						bean.setStatusBar(panelBar.attributeValue("statusBar")
								.split(","));
						bean.setStatusBarConf(panelBar
								.attributeValue("statusBarConf"));
						bean.setToolBar(panelBar.attributeValue("toolBar")
								.split(","));
						bean.setToolBarConf(panelBar
								.attributeValue("toolBarConf"));
						bean.setSystemSet(panelBar.attributeValue("systemSet")
								.split(","));
						bean.setSystemSetConf(panelBar
								.attributeValue("systemSetConf"));
						if (panelBar.attributeValue("menuRightBar") != null) {
							bean.setMenuRightBar(panelBar.attributeValue(
									"menuRightBar").split(","));
						}
						bean.setMenuRightBarConf(panelBar
								.attributeValue("menuRightBarConf"));
						for (Iterator j = panelBar.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							if (component.getName().equals("component")) {
								PanelComponentBean componentBean = new PanelComponentBean();
								componentBean.setName(component
										.attributeValue("name"));
								componentBean.setClassName(component
										.attributeValue("className"));
								componentBean.setPrivilegestr(component
										.attributeValue("privilegestr"));
								componentList.add(componentBean);
							}

						}
						bean.setPanelList(componentList);
					}

					panelConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	public static void initConf(String path, List<String> privilege) {
		try {
			SAXReader reader = new SAXReader();
			Document doc = reader.read(path);
			Element root = (Element) doc.getRootElement();
			if (!root.getName().equals("root")) {
				logger.error("读取root根节点异常");
			}
			if (root != null) {
				for (Iterator i = root.elementIterator(); i.hasNext();) {
					PanelBean bean = new PanelBean();
					Element panelBar = (Element) i.next();
					List<PanelComponentBean> componentList = new ArrayList<PanelComponentBean>();
					if (panelBar.getName().equals("panel")) {
						bean.setName(panelBar.attributeValue("name"));
						bean.setLayoutConf(panelBar
								.attributeValue("layoutConf"));
						bean.setMenuBar(panelBar.attributeValue("menuBar")
								.split(","));
						bean.setMenuBarConf(panelBar
								.attributeValue("menuBarConf"));
						bean.setStatusBar(panelBar.attributeValue("statusBar")
								.split(","));
						bean.setStatusBarConf(panelBar
								.attributeValue("statusBarConf"));
						bean.setToolBar(panelBar.attributeValue("toolBar")
								.split(","));
						bean.setToolBarConf(panelBar
								.attributeValue("toolBarConf"));
						bean.setSystemSet(panelBar.attributeValue("systemSet")
								.split(","));
						bean.setSystemSetConf(panelBar
								.attributeValue("systemSetConf"));
						if (panelBar.attributeValue("menuRightBar") != null) {
							bean.setMenuRightBar(panelBar.attributeValue(
									"menuRightBar").split(","));
						}
						bean.setMenuRightBarConf(panelBar
								.attributeValue("menuRightBarConf"));
						for (Iterator j = panelBar.elementIterator(); j
								.hasNext();) {
							Element component = (Element) j.next();
							if (component.getName().equals("component")) {
								PanelComponentBean componentBean = new PanelComponentBean();
								componentBean.setName(component
										.attributeValue("name"));
								componentBean.setClassName(component
										.attributeValue("className"));
								componentBean.setPrivilegestr(component
										.attributeValue("privilegestr"));
								if (haveAuthority(componentBean, privilege)) {
									componentList.add(componentBean);
								}
							}
						}
						bean.setPanelList(componentList);
					}

					panelConf.put(bean.getName(), bean);
				}
			}
		} catch (DocumentException e) {
			logger.error(path + "解析失败严重错误" + path);
		}
	}

	private static boolean haveAuthority(PanelComponentBean cb,
			List<String> privilege) {
		return cb.getPrivilegestr() == null
				|| cb.getPrivilegestr().trim().equals("")
				|| privilege.contains(cb.getPrivilegestr());
	}

	public static void main(String[] args) {
		initConf("conf/default/panel.xml");
		Set<String> keySet = panelConf.keySet();
		Iterator<String> it = keySet.iterator();
		while (it.hasNext()) {
			String key = it.next();
			PanelBean value = panelConf.get(key);
			System.out.println("layoutConf:" + value.getLayoutConf());
			System.out.println("toolBarConf:" + value.getToolBarConf());
			System.out.println("toolBar:" + value.getToolBar());
			System.out.println("menuBarConf:" + value.getMenuBarConf());
			System.out.println("menuBar:" + value.getMenuBar());
			System.out.println("statusBarConf:" + value.getStatusBarConf());
			System.out.println("statusBar:" + value.getStatusBar());
			System.out.println("systemSetConf:" + value.getSystemSetConf());
			System.out.println("systemSet:" + value.getSystemSet());

			List<PanelComponentBean> a = new ArrayList<PanelComponentBean>();
			a = value.getPanelList();
			if (a != null && a.size() != 0) {

				for (int i = 0; i < a.size(); i++) {
					PanelComponentBean comp = new PanelComponentBean();
					comp = a.get(i);
					System.out.println("componentName:" + comp.getName());
					System.out.println("componentClassName:"
							+ comp.getClassName());
				}
			}
		}
	}

	public static Map<String, PanelBean> getPanelConf() {
		return panelConf;
	}

}
